/**
 * 
 */
package com.vladimirjosu.fhc;

import java.util.ArrayList;
import java.util.TreeMap;

/**
 * @author vladimir.josu@gmail.com
 * 
 */
public class DeadPixels {

	private Integer W;
	private Integer H;
	private Integer P;
	private Integer Q;
	private Integer N;
	private Integer X;
	private Integer Y;
	private Integer a;
	private Integer b;
	private Integer c;
	private Integer d;
	private TreeMap<String, Boolean> mPoints;

	public DeadPixels(String string) {
		String[] strings = string.split(" ");
		W = Integer.valueOf(strings[0]);
		H = Integer.valueOf(strings[1]);
		P = Integer.valueOf(strings[2]);
		Q = Integer.valueOf(strings[3]);
		N = Integer.valueOf(strings[4]);
		X = Integer.valueOf(strings[5]);
		Y = Integer.valueOf(strings[6]);
		a = Integer.valueOf(strings[7]);
		b = Integer.valueOf(strings[8]);
		c = Integer.valueOf(strings[9]);
		d = Integer.valueOf(strings[10]);
		ArrayList<Integer> pX = new ArrayList<Integer>();
		ArrayList<Integer> pY = new ArrayList<Integer>();
		pX.add(X);
		pY.add(Y);
		mPoints = new TreeMap<String, Boolean>();
		mPoints.put(getKey(X, Y), true);
		int xl, yl, x, y;
		for (int i = 1; i < N; i++) {
			xl = pX.get(i - 1);
			yl = pY.get(i - 1);
			x = (xl * a + yl * b + 1) % W;
			y = (xl * c + yl * d + 1) % H;
			pX.add(x);
			pY.add(y);
			mPoints.put(getKey(x, y), true);
		}
	}

	// protected String getKey(Point point) {
	// return "" + point.x + ":" + point.y;
	// }

	public String solve() {
		int result = 0;
		for (int y = 0; y <= H - Q; y++) {
			for (int x = 0; x <= W - P; x++) {
				boolean hasDead = hasDead(x, x + P - 1, y, y + Q - 1);
				if (!hasDead) {
					result++;
				}
			}
		}
		return String.valueOf(result);
	}

	private boolean hasDead(int x1, int x2, int y1, int y2) {
		for (int i = x1; i <= x2; i++) {
			for (int j = y1; j <= y2; j++) {
				if (mPoints.containsKey(getKey(i, j))) {
					// Collections.binarySearch(mPoints, getKey(i, j)) != -1) {
					// Util.log("\n" + mPoints.toString() + " : " + getKey(i,
					// j));
					return true;
				}
			}
		}
		return false;
	}

	private String getKey(int i, int j) {
		return "" + i + ":" + j;
	}
}
